/**
 * 
 */
define(["durandal/app", "jquery", "knockout","durandal/composition",'BMap','panel',
         "http", "i18nCommon","kovholder"],
		function(app, $, ko, composition, baiduMap, panal, http, i18nCommon,kov){

	function model(){
    	var self = this;
    	 
    	var p,_dom;

		composition.addBindingHandler("lawtree-init",{
			init: function(dom){
				p = panal.getPanalByElement(dom);
				_dom = dom;
				map = p.param.map;
				initUI();
				initData();
				p.settings.onClose = function () {
					map.clearOverlays();
				}
		}});
		
		function initUI() {
			loadLawTree()
		}

		function initData() {
		}
 
		function loadLawTree(){
			self.humanTree = $('#laytree-tab').tree({
		          url: cywork.global.rootPath + "law/human/getLevelTree?t="+new Date().getTime(),
		          checkbox: false,
		          cascadeCheck: true,
		          onClick: function (node) {
		        	  map.clearOverlays();
		        	  app.trigger("lawdynamic:lawinfo");
		        	  self.humanId = node.id;
		        	  self.locatePoint(node.id-2);
		          },
		          onDblClick: function (node) {
		              var id = node.id;
		              if (id < 0) {
		                  return;
		              } else {
		                  // that.selectHumanId = id;id
		                  // getHumanInfoByHumanId.ajax({humanId: id}).then(that.putHumanInfo);
		              }
		          }
		      });
		}
		
		self.locatePoint = function(i){
        	var points = [
      			    new BMap.Point(121.555177,29.884123), new BMap.Point(121.564519,29.894393),
      				new BMap.Point(121.584785,29.882118), new BMap.Point(121.57573,29.869842),
      				new BMap.Point(121.587372,29.873976), new BMap.Point(121.558195,29.872223),
      				new BMap.Point(121.572568,29.879363), new BMap.Point(121.604045,29.863328),
      				new BMap.Point(121.595421,29.874728), new BMap.Point(121.566244,29.882494),
      				new BMap.Point(121.585504,29.879488), new BMap.Point(121.540804,29.87811),
      				new BMap.Point(121.605194,29.881116), new BMap.Point(121.597146,29.86007),
      				new BMap.Point(121.54181,29.894644),  new BMap.Point(121.620861,29.872974),
      				new BMap.Point(121.58342,29.831861)
      				];  
        	if(i>=0){
        		var myIcon = new BMap.Icon(cywork.global.rootPath + "style/common/images/lawhuman.png", new BMap.Size(20,40));
        		var marker = new BMap.Marker(points[i],{icon:myIcon});
        		map.addOverlay(marker);
        		marker.addEventListener("click",showLawInfo);
        	}else{
	        	for(var i=0;i<points.length;++i){           		    
	        		var myIcon = new BMap.Icon(cywork.global.rootPath + "style/common/images/lawhuman.png", new BMap.Size(20,40));
	        		var marker = new BMap.Marker(points[i],{icon:myIcon});
	        		map.addOverlay(marker);
	        		marker.addEventListener("click",showLawInfo);
	        	}    
        	}
        };
        
        function showLawInfo(e){
        	self.lawHumanTraj();
        }
        
        self.lawHumanTraj = function () {
            var p = panal.getInstance('view/lawdynamic/lawinfo', {
                width: 300,
                height: 150,
                bottom: '31px',
                right: '2px',
                title: "人员信息",
                modal: false,
                resizable: false
            }, {map: map,humanId:self.humanId});
        };
	}
	
    return model;
});